package com.udi.project.business.news.NewsThread;


import com.udi.project.business.collection.Thread.MarkDate;
import com.udi.project.business.local.di.thread.Endurance;
import com.udi.project.business.local.di.thread.Submit;
import com.udi.project.business.local.gs1.thread.Gs1Thread;
import com.udi.project.business.utils.redis.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
/**
 * @author Admin
 * 子线程消息控制器
 */
@Controller
@RequestMapping("/news")
public class NewsController {


    @Autowired
    private RedisUtils redisUtils;

    /**
    *
     * @Description: 用于AJAX轮询连接请求消息
     * @param
     * @return: String 返回失败信息 或 null
     * 状态码： 优化：DI申报成功后返回消息获取标识数据库记录编号，最小销售单元产品标识
     *  5001：持久化DI子线程出错
     *  5002：DI申报子线程出错
     *  5003：UDI持久化失败
     *  5004: MarkDate 线程 状态更新失败
     * @Date: 2021/8/20 10:35
     */
    @RequestMapping("/information")
    @ResponseBody
    public String information(){
        StringBuffer messg = null;
        //判断消息list是否存在
       if(redisUtils.hasKey("ThreadHint")){
           List<Object> ims = redisUtils.lGet("ThreadHint",0,-1);
           for(Object mess:ims){
               messg.append(mess);
           }
           String stringMessg = "["+messg;
           redisUtils.del("ThreadHint");
           return stringMessg.substring(0,stringMessg.lastIndexOf(","))+"]";
       }
       return null;
    }


    /**
    *
     * @Description: 失败的子线程重新执行
     * @param typeCode 状态码
     * @return: java.lang.String
     * @Date: 2021/8/20 11:18
     */
    @RequestMapping("/reboot")
    @ResponseBody
    public String reboot(List<Integer> typeCode){
        for(Integer code:typeCode){
            if(code==5001){
                Endurance endurance = new Endurance();
                endurance.start();
            }else if(code==5002){
                Submit submit = new Submit();
                submit.start();
            }else if(code==5003){
                Gs1Thread gs1Thread = new Gs1Thread();
                gs1Thread.start();
            }else if(code==5004){
                MarkDate markDate = new MarkDate();
                markDate.start();
            }
        }
        redisUtils.expire("type",10);
        redisUtils.expire("diCode",10);
        redisUtils.expire("deptId",10);
        redisUtils.expire("serial",10);
        redisUtils.expire("logo",10);
        redisUtils.expire("type",10);
        redisUtils.expire("GS1key",10);
        redisUtils.expire("data",10);
        redisUtils.expire("PrintGs1",10);
        return "已重新执行操作";
    }

}
